package com.tianbo.elfii.client.common;

import java.awt.BorderLayout;
import java.awt.Image;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JPanel;

import com.tianbo.elfii.client.common.ElfiiCommonUtil.FeedBackType;
import com.tianbo.elfii.client.common.wlanmanagement.BaseDialogPane;

public class LanStaticIpPane extends BaseDialogPane {

	public ElfiiCommonKeyboard keyboard = null;
	public ElfiiCommonConfigNet netConfig = null;
	public IPSetDialog dialog = null; 

	public LanStaticIpPane(Object obj) {
		super();
		// TODO Auto-generated constructor stub
		
		netConfig = (ElfiiCommonConfigNet)obj;
	
		init();
		
		dialog.setOpaque(false);
		
	}
	
	public void init(){
		System.out.println("init 1 "+ElfiiCommonUtil.getLocalCurrentTime(""));
		initStaticIpDialog();
		System.out.println("init 2 "+ElfiiCommonUtil.getLocalCurrentTime(""));
		initKeyboardPane();
		System.out.println("init 3 "+ElfiiCommonUtil.getLocalCurrentTime(""));
		
	}
	
	public void initStaticIpDialog(){
		dialog = new IPSetDialog();
		dialog.setBounds(287, 30, 450, 350);
		dialog.init();
		dialog.confirmBtn.addActionListener(new ConfirmBtnListener());
		dialog.cancelBtn.addActionListener(new CancelBtnListener());
		this.add(dialog);
	}

	public void initKeyboardPane() {
		keyboard = new ElfiiCommonLoginKeyboard();
		

		JPanel keyPanel = keyboard.createKeyPanel();
		keyPanel.setOpaque(false);

		this.add(keyPanel);
		
		keyPanel.setBounds(150, 420, 735, 300);

		keyboard.createInfoPanel();

		dialog.keyboard = keyboard;
		
		keyboard.isDigiOnly = true;

	}
	
	public void showDialog(){
		super.showDialog();
		dialog.staticIpFields[0][0].requestFocus();
	}
	
	public class ConfirmBtnListener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent e) {
			// TODO Auto-generated method stu
			ElfiiCommonUtil.playFeedbackAudio(FeedBackType.CLICK);
			if(dialog.isUseStaticIP){
				if(dialog.getInputValidity()){
					
					if(ElfiiCommonConfigUtil.isLanAutoIP){
						ElfiiCommonConfigUtil.setLanAutoIP(false);
						ElfiiCommonConfigUtil.lanStaticIP = dialog.getInputIP();
						ElfiiCommonConfigUtil.connectLan();
						
					}
					else{
						IPInfo tmpIPInfo = dialog.getInputIP();
						
						if(!ElfiiCommonConfigUtil.lanStaticIP.equals(tmpIPInfo)){
							ElfiiCommonConfigUtil.lanStaticIP = dialog.getInputIP();
							ElfiiCommonConfigUtil.connectLan();
						}
					}
					saveLanStaticIP();
				
					mainFrame.setContentPane(backPanel);
				}
			}
			else{

				ElfiiCommonConfigUtil.lanStaticIP = dialog.getInputIP();
				
				if(!ElfiiCommonConfigUtil.isLanAutoIP){
					ElfiiCommonConfigUtil.connectLan();
				}
				saveLanStaticIP();
				
				mainFrame.setContentPane(backPanel);
			}
		}
	}
	
	public class CancelBtnListener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent e) {
			// TODO Auto-generated method stu
			
			ElfiiCommonUtil.playFeedbackAudio(FeedBackType.CLICK);
			mainFrame.setContentPane(backPanel);

			mainFrame.validate();
		}
	}
	
	public void saveLanStaticIP(){

		ElfiiCommonConfigUtil.setLanAutoIP(!dialog.isUseStaticIP);
		ElfiiCommonConfigUtil.saveLanStaticIP();
	}

}
